using System;
using System.Collections.Generic;
using UnityEngine;

public class WaveGenerator : MonoBehaviour
{
	private List<float[]> waveBuffers;

	private int bufferCurrentIndex;

	[HideInInspector]
	public float degree;

	private float amplitude;

	private float frequency = 400f;

	private float minimalAmplitude;

	private float maximalAmplitude = 1.2f;

	private float targetAmplitude;

	private float amplitudeChangingSpeed = 4f;

	private float amplitudeZeroingSpeed = 1f;

	private float minimalFrequency = 400f;

	private float maximalFrequency = 700f;

	private float targetFrequency;

	private float frequencyChangingSpeed = 200f;

	private float frequencyZeroingSpeed = 40f;

	private void Awake()
	{
		InitializeWaveBuffer();
		targetAmplitude = 0f;
	}

	public void CreateFillingUpWave(float value)
	{
		float num = (maximalAmplitude - minimalAmplitude) * value + minimalAmplitude;
		if (num <= minimalAmplitude)
		{
			targetAmplitude = minimalAmplitude;
		}
		else
		{
			targetAmplitude = num;
		}
		float num2 = (maximalFrequency - minimalFrequency) * value + minimalFrequency;
		if (num2 <= minimalFrequency)
		{
			targetFrequency = minimalFrequency;
		}
		else
		{
			targetFrequency = num2;
		}
	}

	private void RecalculateWaveParameters()
	{
		if (amplitude < targetAmplitude)
		{
			amplitude += amplitudeChangingSpeed * Time.fixedDeltaTime;
			if (amplitude >= targetAmplitude)
			{
				amplitude = targetAmplitude;
			}
		}
		else if (amplitude > targetAmplitude)
		{
			amplitude -= amplitudeZeroingSpeed * Time.fixedDeltaTime;
			if (amplitude <= targetAmplitude)
			{
				amplitude = targetAmplitude;
			}
		}
		if (frequency < targetFrequency)
		{
			frequency += frequencyChangingSpeed * Time.fixedDeltaTime;
			if (frequency >= targetFrequency)
			{
				frequency = targetFrequency;
			}
		}
		else if (frequency > targetFrequency)
		{
			frequency -= frequencyZeroingSpeed * Time.fixedDeltaTime;
			if (frequency <= targetFrequency)
			{
				frequency = targetFrequency;
			}
		}
	}

	public void GetWaveBuffer(ref float[] buffer, int waveIndex)
	{
		int num = buffer.Length;
		for (int i = 0; i < num; i++)
		{
			int num2 = bufferCurrentIndex - num + i;
			if (num2 < 0)
			{
				num2 = waveBuffers[waveIndex].Length + num2;
			}
			buffer[num - i - 1] = waveBuffers[waveIndex][num2];
		}
	}

	private void FixedUpdate()
	{
		RecalculateWaveParameters();
		GenerateBufferEntry();
	}

	private void GenerateBufferEntry()
	{
		degree += frequency * Time.fixedDeltaTime;
		if (degree > 360f)
		{
			degree -= 360f;
		}
		for (int i = 0; i < waveBuffers.Count; i++)
		{
			float num = (float)Math.PI / 180f * ((float)i * 45f);
			float num2 = Mathf.Sin((float)Math.PI / 180f * degree + num) * amplitude;
			waveBuffers[i][bufferCurrentIndex] = num2;
		}
		bufferCurrentIndex++;
		if (bufferCurrentIndex == 30)
		{
			bufferCurrentIndex = 0;
		}
	}

	private void InitializeWaveBuffer()
	{
		waveBuffers = new List<float[]>();
		CreateNewWaveBuffer(2);
	}

	public void CreateNewWaveBuffer(int amount)
	{
		for (int i = 0; i < amount; i++)
		{
			float[] item = new float[30];
			waveBuffers.Add(item);
		}
	}

	public void SpeedUpZeroingParameters()
	{
		amplitudeZeroingSpeed = 4f;
		frequencyZeroingSpeed *= 4f;
	}

	public float GetCurrentAmplitude()
	{
		return amplitude;
	}
}
